from arq.connections import RedisSettings, create_pool
from fastapi import FastAPI
from afast_core.core_plugins.pluginbase import IBasePlugin as BasePlugin


class ARQPluginClient(BasePlugin):
    # 设置插件默认的参数信息
    name = 'ARQ插件'

    class RedisSettings(RedisSettings):
        pass

    def __init__(self, app: FastAPI = None, name=None, settings=None, **options):
        super().__init__(app, name, settings, **options)
        self._arq_app = None

    def setup(self, app: FastAPI, name: str = None, *args, **kwargs):
        """插件初始化"""
        self.app = app

        @app.on_event("startup")
        async def startup_event():
            self._arq_app = await create_pool(self.settings)
            app.arqcilent = self

    @property
    def enqueue_job(self):
        return self._arq_app.enqueue_job
